package com.itita.ww2.core;

import java.util.Collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.smartfoxserver.v2.extensions.SFSExtension;
import com.itita.ww2.game.user.UserService;
import com.itita.ww2.model.user.UserProfile;

public class WW2ShutDownHook extends Thread {
	private static final Logger logger = LoggerFactory.getLogger(WW2ShutDownHook.class);

	public WW2ShutDownHook() {
		super("WW2 ShutDownHook");
		logger.info(this.getName() + " initialized.");
	}

	@Override
	public void run() {
		super.run();
		logger.info(this.getName() + " shutdown.....");
		// 将所有玩家强退
		SFSExtension ext = ServerService.getInstance().getMainExtension();
		Collection<UserProfile> userProfile = UserService.getInstance().getAllUserProfile();
		for (UserProfile ret : userProfile) {
			if (ret.isLogin()) {
				ret.onDisconnectEvent();
				ext.getApi().kickUser(ret.getSFSUser(), null, "Server Shut down!", 1);
			}
		}
		// redis force landing
		ServerService.getInstance().getRedis().syncToDB(false);
		logger.info(this.getName() + " success!");
	}
}
